<?php
/**
 * Created by PhpStorm.
 * User: kenny
 * Date: 16-4-7
 * Time: 下午3:21
 */
namespace Common\Lib;

/**
 * 阿里云视频上传组件
 * Class VideoUpload
 * @package Common\Lib
 */
class VideoUpload
{
    /**
     * 阿里云图片上传静态类
     * @var \OssClient
     */
    private static $OssClient = null;

    /**
     * 阿里云参考静态类
     * @var \RefererConfig
     */
    private static $RefererConfig = null;

    /**
     * 阿里云访问key_id
     */
    const ACCESS_KEY_ID = 'RDcee3km7r9cfmQf';

    /**
     * 阿里云访问secret
     */
    const ACCESS_KEY_SECRET = 'eckWO6mvoMwNhUBFtiTn2UojP8wUT0';

    /**
     * 阿里云上传
     */
    const HOSTNAME = 'oss-cn-shanghai.aliyuncs.com';

    /**
     * 默认阿里云bucket
     */
    const DEFAULT_BUCKET = 'maxer-video';

    /**
     * 图片上传默认路径
     */
    const IMG_UPLOAD_DEFAULT_PATH = 'Static/video/';

    /**
     * 图片本地保存目录
     */
    const STATIC_IMG_SRC = 'Static/video/';

    /**
     * 视频cdn加速实时裁剪目录
     */
    const STATIC_VIDEO_CDN_URL = 'http://maxer-video.oss-cn-shanghai.aliyuncs.com/';

    /**
     * 视频cdn加速域名
     */
    const STATIC_VIDEO_CDN_SPEED_URL = 'http://video-cdn.max99.cn/';

    /**
     * 获取阿里云OssClient
     * @return \OssClient
     */
    public static function getOssClient() {
        $accessKeyId = self::ACCESS_KEY_ID;
        $accessKeySecret = self::ACCESS_KEY_SECRET;
        $hostname = self::HOSTNAME;
        vendor('aliyunOSS.OssClient');
        if (!empty(self::$OssClient)) {
            return self::$OssClient;
        } else {
            $OssClient = new \OssClient($accessKeyId, $accessKeySecret, $hostname);
            self::$OssClient = $OssClient;
        }

        return self::$OssClient;
    }

    /**
     * 获取阿里云RefererConfig
     * @return \RefererConfig
     */
    public static function getRefererConfig() {
        vendor('aliyunOSS.Model.RefererConfig');
        if (!empty(self::$OssClient)) {
            return self::$OssClient;
        } else {
            $RefererConfig = new \RefererConfig();
            self::$RefererConfig = $RefererConfig;
        }

        return self::$RefererConfig;
    }

    /**
     * 上传对象到阿里云
     * @param $object
     * @param $file
     * @param string $bucket
     * @return null
     * @throws \OssException
     */
    public function aliyunUpload($object, $file, $bucket = self::DEFAULT_BUCKET) {
        $OssClient = self::getOssClient();
        $response = $OssClient->uploadFile($bucket, $object, $file);
        return $response;
    }

    /**
     * 从阿里云删除对象
     * @param $bucket
     * @param $object
     * @return mixed
     */
    public function aliyunDeleteObject($bucket, $object) {
        $OssClient = self::getOssClient();
        $response = $OssClient->deleteObject($bucket, $object);
        return $response;
    }

    /**
     * @param \OssClient $ossClient
     * @param string $bucket
     */
    public function putBucketReferer($ossClient, $bucket)
    {
        $refererConfig = self::getRefererConfig();
        $refererConfig->setAllowEmptyReferer(true);
        $refererConfig->addReferer("www.aliiyun.com");
        $refererConfig->addReferer("www.aliiyuncs.com");
        try{
            $ossClient->putBucketReferer($bucket, $refererConfig);
        } catch(OssException $e) {
            printf(__FUNCTION__ . ": FAILED\n");
            printf($e->getMessage() . "\n");
            return;
        }
        print(__FUNCTION__ . ": OK" . "\n");
    }

    /**
     * 生成GetObject的签名url,主要用于私有权限下的读访问控制
     * @param string $object 阿里云上传相对路径的地址
     * @param int $timeout 超时时间
     * @param string $bucket 阿里云bucket名
     * @return bool|string
     */
    public function getSignedUrlForGettingObject($object, $timeout, $bucket = self::DEFAULT_BUCKET)
    {
        $ossClient = self::getOssClient();
        try{
            $signedUrl = $ossClient->signUrl($bucket, $object, $timeout);
        } catch(\OssException $e) {
            printf(__FUNCTION__ . ": FAILED\n");
            printf($e->getMessage() . "\n");
            return false;
        }

        $signedUrl = str_replace(self::STATIC_VIDEO_CDN_URL, self::STATIC_VIDEO_CDN_SPEED_URL, $signedUrl);

        return $signedUrl;
    }

    /**
     * 获取视频上传路径
     * @return string
     */
    public function getVideoUploadPath() {
        $path = 'training_camp/' . date('Ymd', NOW_TIME) . '/';
        return $path;
    }

    public function uploadFile($dataPath, $fileFullName = '', $uploadPath = self::IMG_UPLOAD_DEFAULT_PATH, $basePath = self::STATIC_IMG_SRC)
    {
        if (empty($dataPath))
            E(L('_USER_UPLOAD_IMG_EMPTY'));

        if (!empty($dataPath) && !empty($uploadPath) && !empty($fileFullName)) {
            if (!is_dir($basePath . $uploadPath)) {
                mkdir($basePath . $uploadPath, 0777, true);
            }
            $uploadSrc = $basePath . $uploadPath . $fileFullName;
            $ret = move_uploaded_file($dataPath, $uploadSrc);
            if ($ret) {
                $uploadFile = $uploadPath . $fileFullName;
                $result['uploadFile'] = $uploadFile;
                $result['uploadSrc'] = $uploadSrc;
                $result['ImageCDNUrl'] = self::STATIC_VIDEO_CDN_URL . $uploadFile;
                return $result;
            }
        }

        return false;
    }

}